package fir;

import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.Clip;

public class Sound {
	private static Object currentSound;
	static String[] filename = { "sound/falldown.wav", "sound/takeback.wav", "sound/newgame.wav",
			              "sound/win.wav", "sound/lose.wav"};

	public static void falldown() {
		try {
                    //currentSound = AudioSystem.getAudioInputStream(new File(filename[0]));
                    currentSound = AudioSystem.getAudioInputStream(ClassLoader.getSystemResource(filename[0]));
		} catch (UnsupportedAudioFileException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (currentSound instanceof AudioInputStream) {
			Clip clip;
			try {
				AudioInputStream stream = (AudioInputStream) currentSound;
				AudioFormat format = stream.getFormat();
				DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat(),
                                        ( (int)stream.getFrameLength() * format.getFrameSize()) );
				clip = (Clip) AudioSystem.getLine(info);
				clip.open(stream);
				currentSound = clip;
			}
			catch (Exception ex) {
				currentSound = null;
			}
			if (currentSound != null){
				clip = (Clip) currentSound;
				clip.start();
			}
		}
	}

	public static void takeback() {
		try {
                    //currentSound = AudioSystem.getAudioInputStream(new File(filename[1]));
                    currentSound = AudioSystem.getAudioInputStream(ClassLoader.getSystemResource(filename[1]));
		} catch (UnsupportedAudioFileException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (currentSound instanceof AudioInputStream) {
			Clip clip;
			try {
				AudioInputStream stream = (AudioInputStream) currentSound;
				AudioFormat format = stream.getFormat();
				DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat(),
                                        ( (int)stream.getFrameLength() * format.getFrameSize()) );
				clip = (Clip) AudioSystem.getLine(info);
				clip.open(stream);
				currentSound = clip;
			}
			catch (Exception ex) {
				currentSound = null;
			}
			if (currentSound != null){
				clip = (Clip) currentSound;
				clip.start();
			}
		}
	}
	
	public static void win() {
		try {
                    //currentSound = AudioSystem.getAudioInputStream(new File(filename[3]));
                    currentSound = AudioSystem.getAudioInputStream(ClassLoader.getSystemResource(filename[3]));
		} catch (UnsupportedAudioFileException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (currentSound instanceof AudioInputStream) {
			Clip clip;
			try {
				AudioInputStream stream = (AudioInputStream) currentSound;
				AudioFormat format = stream.getFormat();
				DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat(),
                                        ( (int)stream.getFrameLength() * format.getFrameSize()) );
				clip = (Clip) AudioSystem.getLine(info);
				clip.open(stream);
				currentSound = clip;
			}
			catch (Exception ex) {
				currentSound = null;
			}
			if (currentSound != null){
				clip = (Clip) currentSound;
				clip.start();
			}
		}
	}
	
	//������ز�
	public static void lose() {
		try {
                    //currentSound = AudioSystem.getAudioInputStream(new File(filename[4]));
                    currentSound = AudioSystem.getAudioInputStream(ClassLoader.getSystemResource(filename[4]));
		} catch (UnsupportedAudioFileException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (currentSound instanceof AudioInputStream) {
			Clip clip;
			try {
				AudioInputStream stream = (AudioInputStream) currentSound;
				AudioFormat format = stream.getFormat();
				DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat(),
                                        ( (int)stream.getFrameLength() * format.getFrameSize()) );
				clip = (Clip) AudioSystem.getLine(info);
				clip.open(stream);
				currentSound = clip;
			}
			catch (Exception ex) {
				currentSound = null;
			}
			if (currentSound != null){
				clip = (Clip) currentSound;
				clip.start();
			}
		}
	}
	
	//������ز�
	public static void newgame() {
		try {
                    //currentSound = AudioSystem.getAudioInputStream(new File(filename[2]));
                    currentSound = AudioSystem.getAudioInputStream(ClassLoader.getSystemResource(filename[2]));
		} catch (UnsupportedAudioFileException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (currentSound instanceof AudioInputStream) {
			Clip clip;
			try {
				AudioInputStream stream = (AudioInputStream) currentSound;
				AudioFormat format = stream.getFormat();
				DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat(),
                                        ( (int)stream.getFrameLength() * format.getFrameSize()) );
				clip = (Clip) AudioSystem.getLine(info);
				clip.open(stream);
				currentSound = clip;
			}
			catch (Exception ex) {
				currentSound = null;
			}
			if (currentSound != null){
				clip = (Clip) currentSound;
				clip.start();
			}
		}
	}
}